﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Scan
{
    public partial class Form2 : Form
    {
        public Form2(string ip)
        {
            InitializeComponent();
            label2.Text = ip;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string ip = label2.Text;
            string port = textBox1.Text;

            dataGridView1.Rows.Clear();

            List<int> ports = new List<int>();
            //计算端口
            if (port.Contains(','))
            {
                if (port.Contains('-'))
                {
                    foreach (string x in port.Split(','))
                    {
                        if (!string.IsNullOrEmpty(x))
                        {
                            if (x.Contains('-'))
                            {
                                string[] tmp = x.Split('-');
                                if (tmp.Length == 2)
                                {
                                    for (int z = int.Parse(tmp[0]);z <= int.Parse(tmp[1]);z++)
                                    {
                                        ports.Add(z);
                                    }
                                } else
                                {
                                    MessageBox.Show("端口范围指定错误");
                                    return;
                                }
                            } else
                            {
                                ports.Add(int.Parse(x));
                            }
                        } else
                        {
                            MessageBox.Show("空字符串");
                            return;
                        }
                    }
                } else
                {
                    foreach (string x in port.Split(','))
                    {
                        ports.Add(int.Parse(x));
                    }
                }
            } else
            {
                if (port.Contains('-'))
                {
                    string[] tmp = port.Split('-');
                    if (tmp.Length != 2)
                    {
                        MessageBox.Show("端口范围指定错误");
                        return;
                    }
                    for (int x = int.Parse(tmp[0]); x <= int.Parse(tmp[1]); x++)
                    {
                        ports.Add(x);
                    }
                } else
                {
                    ports.Add(int.Parse(port));
                }
            }

            TcpClient tc = new TcpClient();
            tc.SendTimeout = tc.ReceiveTimeout = 500;
            progressBar1.Maximum = ports.Count();

            for (int i = 0; i < ports.Count; i++)
            {
                try
                {
                    tc.Connect(ip, ports[i]);
                    if (tc.Connected)
                    {
                        int index = dataGridView1.Rows.Add();
                        dataGridView1.Rows[index].Cells[0].Value = i;
                        dataGridView1.Rows[index].Cells[1].Value = ip;
                        dataGridView1.Rows[index].Cells[2].Value = ports[i];
                        dataGridView1.Rows[index].Cells[3].Value = tc.SendTimeout.ToString();
                    }
                    
                } catch {
                    Console.WriteLine("Port {0}:{1} is Closed", ip, ports[i]);
                } finally
                {
                    label5.Text = ip + ":" + ports[i].ToString();
                    label6.Text = i + " / " + ports.Count();
                    progressBar1.Value++;
                }
                
            }

            tc.Close();
            tc = null;

            MessageBox.Show("端口扫描结束");
        }
    }
}
